package org.jeecg.modules.solitaire_app.app.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.solitaire_app.app.entity.YsyUsers;
import org.jeecg.modules.solitaire_app.app.entity.YsyWalletLog;
import org.jeecg.modules.solitaire_app.app.entity.YsyWithdrawLog;
import org.jeecg.modules.solitaire_app.app.mapper.YsyUsersMapper;
import org.jeecg.modules.solitaire_app.app.mapper.YsyWalletLogMapper;
import org.jeecg.modules.solitaire_app.app.mapper.YsyWithdrawLogMapper;
import org.jeecg.modules.solitaire_app.app.service.IYsyWithdrawLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 提现记录表
 * @Author: jeecg-boot
 * @Date: 2023-09-07
 * @Version: V1.0
 */
@Service
public class YsyWithdrawLogServiceImpl extends ServiceImpl<YsyWithdrawLogMapper, YsyWithdrawLog> implements IYsyWithdrawLogService {

    @Autowired
    private YsyWalletLogMapper ysyWalletLogMapper;
    @Autowired
    private YsyUsersMapper ysyUsersMapper;

    @Override
    public int updateWalletLog(List<YsyWalletLog> ysyWalletLog) {
        return ysyWalletLogMapper.updateWalletLog(ysyWalletLog);
    }

    @Override
    @DS("ysy_db")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public Result<?> toUserFreeToUseMoney() {
        //查询所有运营端用户
        List<YsyWalletLog> ysyWalletLogs = ysyWalletLogMapper.getYsyWalletLogs();

        List<YsyUsers> updateUsers = new ArrayList<>();

        if (ysyWalletLogs.size() > 0) {
            for (int i = 0; i < ysyWalletLogs.size(); i++) {
                YsyUsers ysyUsers = new YsyUsers();
                ysyUsers.setId(ysyWalletLogs.get(i).getUserId());
                ysyUsers.setBalance(ysyWalletLogs.get(i).getMoney());
                updateUsers.add(ysyUsers);
            }

            //批量修改用户资金
            ysyUsersMapper.updateBatch(updateUsers);

            //批量修改资金记录为已审核
            ysyWalletLogMapper.updateWalletLog(ysyWalletLogs);
        }
        return Result.ok("操作成功!");
    }
}
